package com.vallis.calc.util;

import cn.hutool.core.math.MathUtil;
import com.vallis.lottery.domain.LotteryOutPut;
import com.vallis.lottery.domain.LotteryTicket;
import com.vallis.lottery.domain.StandardTicket;

import java.util.*;

/**
 * @author vallis
 */
public class CalcUtil {


    /**
     *
     *
     *
     *
     *
     *
     *基础算法
     *
     *
     *
     *
     *
     *
     *
     *
     */

    /**
     * 将输入的 int数组 转换成 String类型
     */
    public static String[] makeIntArrayToStringArray(int[] checkNumber) {

        String[] checkNumberByStr = new String[checkNumber.length];
        for (int i = 0; i < checkNumber.length; i++) {
            checkNumberByStr[i] = String.valueOf(checkNumber[i]);

        }
        return checkNumberByStr;

    }
    /**
     * 将输入的 String数组 转换成 int类型
     *
     * @param checkNumber
     *
     * @return
     */
    public static int[] makeStringArrayToIntArray(String[] checkNumber) {
        int[] checkNumberByInt = new int[checkNumber.length];
        for (int i = 0; i < checkNumber.length; i++) {
            checkNumberByInt[i] = Integer.parseInt(checkNumber[i]);

        }
        return checkNumberByInt;
    }

    /**
     * 将String 转换成String数组
     *
     * @param string
     *
     * @return
     */
    public static String[] makeStringToStringArray(String string) {
        return string.split(",");
    }

    /**
     * 数组转list
     * @param array
     * @return
     */
    public static List<Integer> makeArrayToIntList(int[] array){
        List<Integer> resultList = new ArrayList<>(array.length);
        for (Integer s : array) {
            resultList.add(s);
        }
        return resultList;
    }



    /**
     * 如果包含中文逗号 一样处理
     * @param string
     * @return
     */
    public static String[] calcStringToStringArray(String string){
        if (string.contains(",")){
            String[] split = string.split(",");
            return split;
        }else if (string.contains("，")){
            String[] split = string.split("，");
            return split;
        }else if (string.contains(" ")){
            String[] split = string.split(" ");
            return split;
        }
        return new String[]{string};
    }

    /**
     * 查询每个String[] 在 List<String[]>中的出现次数
     * @param c
     * @param o
     * @return
     */
    public static int calcStringArrayRepeatTimes(List<String[]> c, String[] o) {
        int result = 0;
        if (o == null) {
            for (String[] e : c){
                if (Arrays.equals(e, o)){
                    result++;
                }

            }

        } else {
            for (String[] e : c) {
                if (Arrays.equals(e, o)) {
                    result++;
                }
            }
        }
        return result;
    }

    /**
     * 数组去重
     * @param lotteryTickets
     * @return
     */
    public  static    List<LotteryTicket> distinct(List<LotteryTicket> lotteryTickets) {

        Set<LotteryTicket> set=new HashSet<>(lotteryTickets);

        List<LotteryTicket> temp=new ArrayList<>(set);

        return temp;



    }

    /**
     *
     *
     *
     * 高阶算法
     *
     *
     *
     *
     */


    /**
     * 计算该实票的价格
     * @param
     * @return
     */
    public static Long getSinglePrice(String[] f, String[] e,int type) {
        if (type==1){
            long fp = MathUtil.combinationCount(f.length, 5);
            long ep = MathUtil.combinationCount(e.length, 2);
            return fp * ep * 2;
        }else {
            long fp = MathUtil.combinationCount(f.length, 6);
            long ep = MathUtil.combinationCount(e.length, 1);

            return fp * ep * 2;
        }

    }

    /**
     * 将单注彩票分解成N个0+N
     *
     * @param superLotto
     *
     * @return
     */
    public static List<String[]> decomposedEnd(LotteryTicket superLotto, int n) {

        return MathUtil.combinationSelect(calcStringToStringArray(superLotto.getEndNum()), n);
    }

    /**
     * 将单注彩票分解成N个N+0
     *
     * @param superLotto
     *
     * @return
     */
    public static List<String[]> decomposedFront(LotteryTicket superLotto, int n) {
        String[] checkNumber = calcStringToStringArray(superLotto.getFrontNum());

        return MathUtil.combinationSelect(checkNumber, n);
    }

    /**
     * 将此拆解成标准的5+2形式、6+1形式
     * 0->5+2
     * 1->6+1
     * @param superLotto
     *
     * @return
     */
    public static List<StandardTicket> decomposed(LotteryTicket superLotto ,int type) {
        List<String[]> allFist = null;
        List<String[]> allBack = null;
        if (type==0){
             allFist = MathUtil.combinationSelect(calcStringToStringArray(superLotto.getFrontNum()), 5);
           allBack = MathUtil.combinationSelect(calcStringToStringArray(superLotto.getEndNum()), 2);
        }else if(type==1){
             allFist = MathUtil.combinationSelect(calcStringToStringArray(superLotto.getFrontNum()), 6);
             allBack = MathUtil.combinationSelect(calcStringToStringArray(superLotto.getEndNum()), 1);

        }

        List<StandardTicket> tickets = new ArrayList<>();
        StandardTicket  standardTicket = null;
        for (String[] first : allFist) {
            for (String[] back : allBack) {

                StringBuilder firstString=new StringBuilder();
                StringBuilder endString=new StringBuilder();

                for (int i = 0; i < first.length; i++) {
                    if (i==(first.length-1)){
                        firstString.append(first[i]);
                    }else if (i<=(first.length-2)){
                        firstString.append(first[i]+",");
                    }
                }
                for (int i = 0; i < back.length; i++) {
                    if (i==back.length-1){
                        endString.append(back[i]);
                    }else if (i<=back.length-2){
                        endString.append(back[i]+",");
                    }
                }
                standardTicket   =new StandardTicket();
                if (type==0){
                    standardTicket.setTicketType(0L);
                }else if(type==1){
                    standardTicket.setTicketType(1L);
                }

                String[] frontNum = calcStringToStringArray(firstString.toString());
                String[] endNum = calcStringToStringArray(endString.toString());
                standardTicket.setFirstFront(Long.parseLong(frontNum[0]));
                standardTicket.setSecondFront(Long.parseLong(frontNum[1]));
                standardTicket.setThirdFront(Long.parseLong(frontNum[2]));
                standardTicket.setFourthFront(Long.parseLong(frontNum[3]));
                standardTicket.setFifthFront(Long.parseLong(frontNum[4]));
                standardTicket.setFirstEnd(Long.parseLong(endNum[0]));
                standardTicket.setSecondEnd(Long.parseLong(endNum[1]));
                standardTicket.setNum(Long.parseLong(superLotto.getOrderNum()));


                tickets.add(standardTicket);
            }
        }
        return tickets;
    }



  public static void main(String[] args) {
      //
      LotteryTicket superLotto=new LotteryTicket();
      superLotto.setFrontNum("1,2,3,4,5,6,7");
      superLotto.setEndNum("1,2,3");
      superLotto.setOrderNum("21534");


  }
}
